<?php

class NewUserSummaryTask extends sfBaseTask
{
    protected function configure()
    {
        $this->addOptions(array(
            new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environement', 'prod'),
        ));
        
        $this->namespace = 'updates';
        $this->name = 'new-users';
        $this->briefDescription = 'If there\'s been new users, send a notification email to the admin';
        $this->detailedDescription = '';
    }
    
    protected function execute($arguments = array(), $options = array())
    {
        // Required to allow database queries
        $databaseManager = new sfDatabaseManager($this->configuration);
        
        $newUsers = Doctrine_Core::getTable('User')
                ->createQuery('u')
                ->where('u.created_date > ?', date('Y-m-d H:i:s', (time() - Config::$newUserSummaryFrequency)))
                ->orderBy('u.created_date')
                ->execute();
        if ($newUsers->count() > 0) {
            
            $newUsersEmailHtml = "";
            foreach ($newUsers as $newUser) {
                $newUsersEmailHtml .= "<li>" . $newUser->getEmail() . " (" . ($newUser->getActive() ? "" : "not ") . "active)</li>";
            }
            
            $emailHtml = "New users added in the last " . (Config::$newUserSummaryFrequency / 60 / 60) . " hours:<ul>" . $newUsersEmailHtml . "</ul>";
            
            EmailHelper::send("cameron.ross@gmail.com", "New Fydal users", $emailHtml);
        }
        
        echo "Done." . PHP_EOL;
    }
}